package com.symria.system.constants;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public enum ATTResource {
	  IMAGE("IMG","图片","(jpg|png|bmp|gif)"),
	  MUSIC("MUSIC","音乐","(mp3|wav|wma|m4a|aac)"),
	  VIEW("VIEW","视频","(mpeg|avi|mov|wmv|3gp|flv|rmvb|mp4)"),
	  MTEXT("MTEXT","歌词","(lrc|ksc|wrc)"),
	  DOC("DOC","文档","(doc|xls)");//默认格式
	  public static final Map<String, ATTResource> statusMap = new HashMap<String, ATTResource>();
	  public static final List<Object[]> statusList = new ArrayList<Object[]>();
		static {
			ATTResource[] statu = ATTResource.class.getEnumConstants();
		    for(ATTResource s:statu ) {
		    	statusMap.put(s.toString(), s);
		    	statusList.add(new Object[]{s.value,s.name});
		    }
	  }
	  public static ATTResource fromValue(String value) {
		  return statusMap.get(value);
	  }
	  public static ATTResource fromPath(String filename,ATTResource def) {
		  String suffix=getExtensionName(filename);
		  ATTResource r = fromSuffix(suffix);
		  return r==null?def:r;
	  }
	  private static ATTResource fromSuffix(String suffix){
		  ATTResource[] statu = ATTResource.class.getEnumConstants();
		  for(ATTResource s:statu ) {
			  Pattern p = Pattern.compile(s.suffix, Pattern.CASE_INSENSITIVE);
				Matcher m = p.matcher(suffix);
				if(m.matches()){
					return s;
				}
		  }
          return null;
	  }
	  private static String getExtensionName(String filename) {
			if ((filename != null) && (filename.length() > 0)) {
				int i = filename.lastIndexOf('.');
				if ((i > -1) && (i < (filename.length() - 1))) {
					return filename.substring(i + 1);
				}
			}
			return null;
		}
	  @Override
	  public String toString() {
			return this.value==null?"":this.value.toString();
	  }
	  public String toValue(){
		    return this.value;
	  }
	  public String toName(){
		  return this.name;
	  }
	  private String value;
	  private String name;
	  private String suffix;
	  ATTResource(String value,String name,String suffix) {
	    this.value = value;
	    this.name = name;
	    this.suffix=suffix;
	  }
}
